package com.aboo.util;

/**
 * 数据工具包
 * @author Yata Aboo
 *
 */
public class DataUtil {

	/**
	 * 获取int数据
	 * @param value  待处理的数据
	 * @param defValue  默认值
	 * @return
	 */
	public static final int getInt(Object value, int defValue) {
		if (value == null) {
			return defValue;
		}

		int result = defValue;
		if (value instanceof Number) {
			result = ((Number) value).intValue();
		}
		else if (value instanceof String) {
			String str = (String) value;
			try {
				int radix = 10;
				if (str.length() > 2 && str.charAt(0) == '0') {
					char c = str.charAt(1);
					switch (c) {
						case 'x':
						case 'X':
							radix = 16;
							break;
						case 'o':
						case 'O':
							radix = 8;
							break;
						case 'b':
						case 'B':
							radix = 2;
							break;
					}
					if (radix != 10) {
						str = str.substring(2);
					}
				}
				result = Integer.parseInt(str, radix);
			}
			catch (Exception ex) {
				result = defValue;
			}
		}
		else if (value instanceof String[]) {
			String[] strs = (String[]) value;
			if (strs.length > 0) {
				result = getInt(strs[0], defValue);
			}
			else {
				result = defValue;
			}
		}
		else if (value instanceof Boolean) {
			Boolean b = (Boolean) value;
			return b.booleanValue() ? 1 : 0;
		}
		else {
			result = defValue;
		}

		return result;
	}

	/**
	 * 获取long数据
	 * @param value  待处理的数据
	 * @param defValue  默认值
	 * @return
	 */
	public static final long getLong(Object value, long defValue) {
		if (value == null) {
			return defValue;
		}

		long result = defValue;
		if (value instanceof Number) {
			result = ((Number) value).longValue();
		}
		else if (value instanceof String) {
			String str = (String) value;
			try {
				int radix = 10;
				if (str.length() > 2 && str.charAt(0) == '0') {
					char c = str.charAt(1);
					switch (c) {
						case 'x':
						case 'X':
							radix = 16;
							break;
						case 'o':
						case 'O':
							radix = 8;
							break;
						case 'b':
						case 'B':
							radix = 2;
							break;
					}
					if (radix != 10) {
						str = str.substring(2);
					}
				}
				result = Long.parseLong(str, radix);
			}
			catch (Exception ex) {
				result = defValue;
			}
		}
		else if (value instanceof String[]) {
			String[] strs = (String[]) value;
			if (strs.length > 0) {
				result = getLong(strs[0], defValue);
			}
			else {
				result = defValue;
			}
		}
		else if (value instanceof Boolean) {
			Boolean b = (Boolean) value;
			return b.booleanValue() ? 1l : 0l;
		}
		else {
			result = defValue;
		}

		return result;
	}

	/**
	 * 获取double数据
	 * @param value  待处理的数据
	 * @param defValue  默认值
	 * @return
	 */
	public static final double getDouble(Object value, double defValue) {
		if (value == null) {
			return defValue;
		}

		double result = defValue;
		if (value instanceof Number) {
			result = ((Number) value).doubleValue();
		}
		else if (value instanceof String) {
			String str = (String) value;
			try {
				result = Double.parseDouble(str);
			}
			catch (Exception ex) {
				result = defValue;
			}
		}
		else if (value instanceof String[]) {
			String[] strs = (String[]) value;
			if (strs.length > 0) {
				result = getDouble(strs[0], defValue);
			}
			else {
				result = defValue;
			}
		}
		else if (value instanceof Boolean) {
			Boolean b = (Boolean) value;
			return b.booleanValue() ? 1d : 0d;
		}
		else {
			result = defValue;
		}

		return result;
	}

	/**
	 * 获取String数据
	 * @param value  待处理的数据
	 * @param defValue  默认值
	 * @return
	 */
	public static final String getString(Object value, String defValue) {
		if (value == null) {
			return defValue;
		}

		String str = defValue;
		if (value instanceof String) {
			str = (String) value;
		}
		else if (value instanceof String[]) {
			String[] values = (String[]) value;
			str = values.length > 0 ? values[0] : value.toString();
		}
		else {
			str = value.toString();
		}
		return str;
	}
	
}